package com.jinxin.platform.cdsmsmgr.alisms;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-07-01
 * @Author: Lingnan
 **/

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsmsmgr.pojo.entity.*;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短信发送工厂类
 *
 *
 */
public class SmsFactory {

    /**
     * 短信发送,支持向多个不同的手机号码发送同样内容的短信
     * @param map
     * @return
     * @throws Exception
     */
    public static Map<String, Object> sendSms(Map<String, Object> map) throws Exception {
        if(ObjectUtils.isEmpty(map.get(SmsConstant.SMS_TYPE))) {
            throw new RuntimeException("参数smsType不能为空!");
        }
        Map<String, Object> resultMap = new HashMap<>();
        if(SmsTypeEnum.aliSms.equals(map.get(SmsConstant.SMS_TYPE))) {
            resultMap = aliSendSms(map);
        }
        return resultMap;
    }

    /**
     * 阿里短信发送(单条)
     * @param map
     * @return
     * @throws ClientException
     * @throws ServerException
     * @throws Exception
     */
    public static Map<String, Object> aliSendSms(Map<String, Object> map) throws ServerException, ClientException {
        if(map.get(SmsConstant.ACCESS_KEY_ID) == null || map.get(SmsConstant.ACCESS_SECERT) == null
                || map.get(SmsConstant.PHONE_NUMBERS) == null || map.get(SmsConstant.SIGN_NAME) == null
                || map.get(SmsConstant.TEMPLATE_CODE) == null) {
            throw new RuntimeException("参数accessKeyId、accessSecert、手机号码、签名和模板CODE不能为空!");
        }
        String appKey = map.get(SmsConstant.ACCESS_KEY_ID).toString();
        String masterSecret = map.get(SmsConstant.ACCESS_SECERT).toString();
        AliSms aliSms = getAliSmsService(appKey, masterSecret);
        // 接收短信的手机号码
        String phoneNumbers = map.get(SmsConstant.PHONE_NUMBERS).toString();
        // 短信签名名称
        String signName = map.get(SmsConstant.SIGN_NAME).toString();
        // 短信模板ID
        String templateCode = map.get(SmsConstant.TEMPLATE_CODE).toString();
        // 短信模板变量对应的实际值
        String templateParam = map.get(SmsConstant.TEMPLATE_PARAM) == null ? null : map.get(SmsConstant.TEMPLATE_PARAM).toString();
        // 上行短信扩展码
        String smsUpExtendCode = map.get(SmsConstant.SMS_UP_EXTEND_CODE) == null ? null : map.get(SmsConstant.SMS_UP_EXTEND_CODE).toString();
        // 外部流水扩展字段
        String outId = map.get(SmsConstant.OUT_ID) == null ? null : map.get(SmsConstant.OUT_ID).toString();
        CommonResponse response = aliSms.sendSms(phoneNumbers, signName, templateCode, templateParam, smsUpExtendCode, outId);
        SmsSendResult responseData = JSONObject.parseObject(response.getData(), SmsSendResult.class);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(SmsConstant.RESULT, responseData);
        return resultMap;
    }

    /**
     * 阿里短信发送(批量)
     * @param map
     * @return
     * @throws ClientException
     * @throws ServerException
     * @throws Exception
     */
    public static Map<String, Object> aliSendBatchSms(Map<String, Object> map) throws ServerException, ClientException {
        if(map.get(SmsConstant.ACCESS_KEY_ID) == null || map.get(SmsConstant.ACCESS_SECERT) == null
                || map.get(SmsConstant.PHONE_NUMBER_JSON) == null || map.get(SmsConstant.SIGN_NAME_JSON) == null
                || map.get(SmsConstant.TEMPLATE_CODE) == null) {
            throw new RuntimeException("参数accessKeyId、accessSecert、手机号码、签名和模板CODE不能为空!");
        }
        String appKey = map.get(SmsConstant.ACCESS_KEY_ID).toString();
        String masterSecret = map.get(SmsConstant.ACCESS_SECERT).toString();
        AliSms aliSms = getAliSmsService(appKey, masterSecret);
        // 接收短信的手机号码
        String phoneNumberJson = map.get(SmsConstant.PHONE_NUMBER_JSON).toString();
        // 短信签名名称
        String SignNameJson = map.get(SmsConstant.SIGN_NAME_JSON).toString();
        // 短信模板ID
        String templateCode = map.get(SmsConstant.TEMPLATE_CODE).toString();
        // 短信模板变量对应的实际值
        String templateParamJson = map.get(SmsConstant.TemplateParamJson) == null ? null :
                map.get(SmsConstant.TemplateParamJson).toString();
        // 上行短信扩展码
        String smsUpExtendCodeJson = map.get(SmsConstant.SMS_UP_EXTEND_CODE_JSON) == null ? null : map.get(SmsConstant.SMS_UP_EXTEND_CODE_JSON).toString();
        CommonResponse response = aliSms.sendBatchSms(phoneNumberJson, SignNameJson, templateCode, templateParamJson, smsUpExtendCodeJson);
        SmsSendResult responseData = JSONObject.parseObject(response.getData(), SmsSendResult.class);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(SmsConstant.RESULT, responseData);
        return resultMap;
    }

    public static JsonResult aliQuerySmsTemplate(Map<String, Object> map) throws ClientException {
        if(map.get(SmsConstant.ACCESS_KEY_ID) == null || map.get(SmsConstant.ACCESS_SECERT) == null
                || map.get(SmsConstant.TEMPLATE_CODE) == null) {
            return JsonResult.error("参数accessKeyId、accessSecert和templateCode不能为空");
        }
        String appKey = map.get(SmsConstant.ACCESS_KEY_ID).toString();
        String masterSecret = map.get(SmsConstant.ACCESS_SECERT).toString();
        AliSms aliSms = getAliSmsService(appKey, masterSecret);
        // 短信签名名称
        String templateCode = map.get(SmsConstant.TEMPLATE_CODE).toString();
        CommonResponse response = aliSms.querySmsTemplate(templateCode);
        Template template = JSONObject.parseObject(response.getData(), Template.class);
        return JsonResult.ok("success",template);
    }
    // 查询短信模板列表
    public static TemplateInfo aliQuerySmsTemplateList(Map<String, Object> map) throws ClientException {
        if(map.get(SmsConstant.ACCESS_KEY_ID) == null || map.get(SmsConstant.ACCESS_SECERT) == null) {
            throw new RuntimeException("参数accessKeyId、accessSecert不能为空!");
        }
        String appKey = map.get(SmsConstant.ACCESS_KEY_ID).toString();
        int pageIndex = map.containsKey(SmsConstant.QUERY_PAGE_INDEX) ?
                (Integer) map.get(SmsConstant.QUERY_PAGE_INDEX) : SmsConstant.PAGE_INDEX;
        int pageSize = map.containsKey(SmsConstant.QUERY_PAGE_SIZE) ?
                (Integer) map.get(SmsConstant.QUERY_PAGE_SIZE) : SmsConstant.PAGE_SIZE;
        String masterSecret = map.get(SmsConstant.ACCESS_SECERT).toString();
        AliSms aliSms = getAliSmsService(appKey, masterSecret);
        CommonResponse response = aliSms.querySmsTemplateList(pageIndex,pageSize);
        return JSONObject.parseObject(response.getData(), TemplateInfo.class);
    }
    // 查询短信模板的审核状态
    public static Map<String, Object> aliQuerySmsSign(Map<String, Object> map) throws ClientException {
        if(map.get(SmsConstant.ACCESS_KEY_ID) == null || map.get(SmsConstant.ACCESS_SECERT) == null
                || map.get(SmsConstant.SIGN_NAME) == null) {
            throw new RuntimeException("参数accessKeyId、accessSecert和signName不能为空!");
        }
        String appKey = map.get(SmsConstant.ACCESS_KEY_ID).toString();
        String masterSecret = map.get(SmsConstant.ACCESS_SECERT).toString();
        AliSms aliSms = getAliSmsService(appKey, masterSecret);
        // 短信签名名称
        String signName = map.get(SmsConstant.SIGN_NAME).toString();
        CommonResponse response = aliSms.querySmsSign(signName);
        SignResult signResult = JSONObject.parseObject(response.getData(), SignResult.class);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(SmsConstant.RESULT, signResult);
        return resultMap;
    }

    // 查询短信签名列表
    public static AliSign aliQuerySmsSignList(Map<String, Object> map) throws ClientException {
        if(map.get(SmsConstant.ACCESS_KEY_ID) == null || map.get(SmsConstant.ACCESS_SECERT) == null) {
            throw new RuntimeException("参数accessKeyId和accessSecert不能为空!");
        }
        String appKey = map.get(SmsConstant.ACCESS_KEY_ID).toString();
        String masterSecret = map.get(SmsConstant.ACCESS_SECERT).toString();
        int pageIndex = map.containsKey(SmsConstant.QUERY_PAGE_INDEX) ?
                (Integer) map.get(SmsConstant.QUERY_PAGE_INDEX) : SmsConstant.PAGE_INDEX;
        int pageSize = map.containsKey(SmsConstant.QUERY_PAGE_SIZE) ?
                (Integer) map.get(SmsConstant.QUERY_PAGE_SIZE) : SmsConstant.PAGE_SIZE;
        AliSms aliSms = getAliSmsService(appKey, masterSecret);
        CommonResponse response = aliSms.querySmsSignList(pageIndex,pageSize);
        return JSONObject.parseObject(response.getData(), AliSign.class);
    }

    // 申请短信签名
    public static AddAliSignResult addSmsSign(Map<String, Object> map,boolean isApply) throws ClientException {
        if(map.get(SmsConstant.ACCESS_KEY_ID) == null || map.get(SmsConstant.ACCESS_SECERT) == null) {
            throw new RuntimeException("参数accessKeyId和accessSecert不能为空!");
        }
        String appKey = map.get(SmsConstant.ACCESS_KEY_ID).toString();
        String masterSecret = map.get(SmsConstant.ACCESS_SECERT).toString();
        String signName = map.get(SmsConstant.SIGN_NAME).toString();
        String signSource = map.get(SmsConstant.SIGN_SOURCE).toString();
        String remark = map.get(SmsConstant.SIGN_REMARK).toString();
        String signType = map.get(SmsConstant.SIGN_TYPE).toString();
        List<SignFile> signFileList = (List<SignFile>) map.get(SmsConstant.SIGN_FILE);
        AliSms aliSms = getAliSmsService(appKey, masterSecret);
        CommonResponse response;
        if(isApply){
            response = aliSms.addSmsSign(signName,signSource,remark,signFileList,signType);
        }else {
            response = aliSms.modifySmsSign(signName,signSource,remark,signFileList,signType);
        }
        return JSONObject.parseObject(response.getData(), AddAliSignResult.class);
    }

    /**
     * 获取阿里短信服务
     * @param accessKeyId
     * @param accessSecert
     * @return
     */
    private static AliSms getAliSmsService(String accessKeyId, String accessSecert) {
        return new AliSms(accessKeyId, accessSecert);
    }

}
